<template>
  <view class="box_17 flex-col">
    <view class="add_head">
      <text class="text_16">求职意向</text>
      <view @click="addexper">
        <image class="label_3" referrerpolicy="no-referrer" src="@/assets/lanhu_66wodebianjigerenziliao/add.png" />
      </view>
    </view>
    <view v-if="query?.intentions?.length">
      <view class="workdetail" v-for="(item, ind) in query?.intentions" :key="item">
        <view class="block_1 flex-row justify-between align-center">
          <view @click="deitorworkdetail(item)">
            <text class="text_17">期望职位:{{ item.position }}</text>
            <text class="text_17 ml_20 ml_40">期望城市:{{ item.city }}</text>
          </view>
          <!-- <text class="text_17" @click="deitorworkdetail(item)">期望城市:{{ item.city }}</text> -->

          <view class="image-wrapper_2 flex-col" @click="deleteimg(ind)">
            <image
              class="thumbnail_15"
              referrerpolicy="no-referrer"
              src="@/assets/lanhu_66wodebianjigerenziliao/delete.png"
            />
          </view>
        </view>
        <view class="block_2 cloumn flex-row" @click="deitorworkdetail(item)">
          <text class="text_19">期望专业:{{ item?.industry }}</text>
        </view>
        <view class="text-wrapper_11 flex-row" @click="deitorworkdetail(item)">
          <text class="text_23">期望描述：{{ item?.descriptions }}</text>
        </view>
      </view>
    </view>
    <emptyimg v-if="!query?.intentions?.length"></emptyimg>
    <view class="section_13 flex-row justify-between">
      <view class="text-wrapper_5 border flex-col" @click="cancel()">
        <text class="text_11">取消</text>
      </view>
      <view class="text-wrapper_6 flex-col" @click="submit">
        <text class="text_12">保存</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import '@/static/common.less';
import './index.less';
import Emptyimg from "@/components/empty-img/index.vue"
import { getCurrentInstance, ref, toRaw, markRaw, reactive } from 'vue';
import Taro, {
  useLoad,
  usePullDownRefresh,
  useReachBottom,
  useShareAppMessage,
  useShareTimeline,
  useTitleClick,
  useDidShow,
} from '@tarojs/taro';

import { CategoryType, getCategoriesApi } from '@/api/categories';
import { loginapi } from '@/api/platform';
import { recruitfavor, applylist, resumedetail, ecruitresume } from '@/api/moredetail';

// import LoadMoreBottom from '@/components/load-more-bottom/index.vue';
import { getProtoListApi, ProtoType,Infouser } from '@/api/proto';
import { showerr, geyDay } from '@/utils/common';
import { Empty } from '@nutui/nutui-taro';

// const windowInfo = Taro.getWindowInfo();
// const { proxy } = getCurrentInstance();
const useInfo = Taro.getStorageSync('useInfo');
let iscreateword = true
const query = ref({
  intentions:[]
});
let baseinfo = reactive({ intentions: [] });
// const intentions = ref([]);
let obj = Taro.getStorageSync('seek');
if (obj) {
  //教育经历
  let data = JSON.parse(obj);
  query.value.intentions = data;
}
useDidShow(() => {
  let obj = Taro.getStorageSync('seeksave');
  if (obj) {
    Taro.removeStorageSync('seeksave');
    //教育经历
    let data = JSON.parse(obj);
    let findindex = query.value.intentions.findIndex((item) => item.id == data?.id);
    if (findindex > -1 && data?.id) {
      query.value.intentions[findindex] = data;
    } else {
      delete data.id;
      let arr = toRaw(query.value.intentions);
      query.value.intentions = arr.concat([data]);
    }
  }
});

const deleteimg = (ind) => {
  query.value.intentions.splice(ind, ind + 1, 0);
  let arr = [];
  query.value.intentions.forEach((item, index) => {
    if (index !== ind) {
      arr.push(item);
    }
  });
  query.value.intentions = arr;
};

const categories = ref<CategoryType[]>([]);
const protoItems = ref<ProtoType[]>([]);
const tab1value = ref('0');
// const protoTotal = ref<number>(0);
const favtorgroup = ref([]);

const addexper = () => {
  if(iscreateword){
    console.log(iscreateword)
    return showerr('请先创建简历')
  }
  Taro.navigateTo({ url: `/pages/userinfo/seekdetail/index` });
};
const deitorworkdetail = (detail) => {
  console.log(detail);
  Taro.setStorageSync('seekdetail', JSON.stringify(detail));
  Taro.navigateTo({ url: `/pages/userinfo/seekdetail/index` });
};
const handleProtoClick = (proto: ProtoType) => {
  console.log(handleProtoClick);
};
const godetail = (id) => {
  Taro.navigateTo({ url: `/pages/workdetail/index?id=${id}` });
};

const cancel = () => {
  Taro.navigateBack();
};

const getbase = () => {
  let usercontent = useInfo && JSON.parse(useInfo);
  resumedetail({ id: usercontent?.id || 4 }).then((res: any) => {
    let arr = Object.keys(res);
    if(arr?.length){
      console.log(length)
      iscreateword = false
    } else {
      return showerr('请先创建简历');
    }
    query.value.intentions = res.intentions;
    res.user_id =  usercontent?.id
    baseinfo = res;

  });
};

getbase();
const submit = () => {
  let result = query.value.intentions.length;
  if (!result){
    return showerr('请添加求职意向');
  }
  let totalinfo = toRaw(baseinfo);
  let queryinfo = toRaw(query.value.intentions);
  totalinfo.intentions = queryinfo;
  let usercontent = useInfo && JSON.parse(useInfo);
  ecruitresume({ id: usercontent?.id || 4, params: totalinfo }).then((res: any) => {
    showerr(res?.message);
    if (res.code === 1010) {
      Taro.navigateBack();
    } 
  });
};
</script>
